<template>
  <div class="app-container">
    <div v-if="showSearch" class="search-container">
      <CommonSearch
        :formConfig="searchColumns"
        :inline="true"
        labelWidth="35px"
        formItemWidth="auto"
        :labelPosition="'left'"
        :model="searchParam"
      >
        <template #after>
          <el-button type="primary" @click="beforeSearch">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
        </template>
      </CommonSearch>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button
          @click="
            useDownload('/mdm/mdmmaterial/export', '呼叫信息统计', searchParam)
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template>
      <!-- 表格主体 -->
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        v-bind="$attrs"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        @sort-change="onTableSortChanged"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->

        <!-- 表格内容操作 -->
        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 列设置 抽屉组件 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDictionary, useDownload } from "@/utils/useTable";
import request from "@/utils/request";
defineOptions({
  name: "ANDONANDCALLCOUNTSTATISTICAL",
  inheritAttrs: false,
});
// 请求参数
const requestApi = "/andon/andcall/selectStatisticsByPage";

// 是否需要分页
const pagination = true;

const initParam = reactive({ sort: {} });

// 初始化搜索参数数据
const searchParamInit = {
  year: new Date().getFullYear(),
};

// 数据请求错误后处理
function requestError() {
  // tableData.value = [
  //   {
  //     id: "12232312",
  //     plantCode: "1212",
  //     workCenterCode: "121vvx2",
  //     workCenterName: "121vxxxxxxx2",
  //     year: 2024,
  //     monthcounts: [12, 23, 45, 0, 12, 0], // 共12个月
  //   },
  // ];
}

// 数据请求错误后处理
// function dataCallBack() {}   //需要处理
const dataCallBack = ref(undefined); //无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError
);

// 监听表格排序方法
const onTableSortChanged = (column: any) => {};

const month = [
  "1月",
  "2月",
  "3月",
  "4月",
  "5月",
  "6月",
  "7月",
  "8月",
  "9月",
  "10月",
  "11月",
  "12月",
];

const tableColumns = reactive([
  {
    prop: "plantCode",
    label: "工厂/工作中心",
    isShow: true,
    minWidth: 300,
    render: (scope: any) => (
      <span>
        {`${scope.row?.plantCode ?? ""}/${scope.row.workCenterCode ?? ""}-${
          scope.row.workCenterName ?? ""
        }`}
      </span>
    ),
  },
  // {
  //   prop: "workCenterCode",
  //   label: "工作中心",
  //   isShow: true,
  //   minWidth: 180,
  //   render: (scope: any) => (
  //     <span>{`${scope.row.workCenterCode ?? ""}-${
  //       scope.row.workCenterName ?? ""
  //     }`}</span>
  //   ),
  // },
  ...month.map((item, index) => ({
    prop: `month${index + 1}`,
    label: item,
    isShow: true,
    minWidth: 90,
    headerAlign: "center",
    align: "right",
    render: (scope: any) => <span>{scope?.row?.monthcounts[index] ?? ""}</span>,
  })),
]);

const getYearList = async () => {
  const { data } = await request({
    method: "POST",
    url: "/system/dateInfo/getYearList",
  });
  searchColumns[0].optionList = data.map((item: any) => ({
    label: item,
    value: item,
  }));
};

// 是否显示搜索框
const showSearch = ref(true);
// 搜索参数 设置为响应式
const searchColumns: any = reactive([
  {
    type: "select",
    label: "年度",
    isShow: true,
    fieldName: "year",
    placeholder: "请选择年度",
    initValue: null,
    defaultFirstOption: true,
    filterable: true,
    optionList: [],
  },
]);

function beforeSearch() {
  if (!searchParam.value.year) {
    ElMessage.error("请选择年份");
  } else {
    search();
  }
}
onMounted(async () => {
  await Promise.all([getYearList(), getTableList()]);
});
</script>

<style scoped lang="scss"></style>
